﻿using System;
using System.Collections.Generic;
using WindowsGame.Common.Data;
using WindowsGame.Common.Static;

namespace WindowsGame.Common.Managers
{
	public interface IConfigManager
	{
		void Initialize();
		void Initialize(String root);
		void LoadContent();
		void LoadBoardData();
		void LoadConfigData();
		void LoadSnakeData();

		BoardType BoardType { get; }
		ConfigData ConfigData { get; }
		SnakeData[] SnakeData { get; }
	}

	public class ConfigManager : BaseManager, IConfigManager
	{
		public void Initialize()
		{
			BaseData.Initialize();
		}
		public void Initialize(String root)
		{
			BaseData.Initialize(root);
		}

		public void LoadContent()
		{
			LoadBoardData();
			LoadConfigData();
			LoadSnakeData();
			BaseData.LoadContent();
		}

		public void LoadBoardData()
		{
			String file = GetGlobalConfigFile(Constants.BOARD_DATA_FILENAME);
			IList<String> lines = MyGame.Manager.FileManager.LoadTxt(file);
			BoardType = (BoardType)Enum.Parse(typeof(BoardType), lines[0], true);
		}
		public void LoadConfigData()
		{
			String file = GetGlobalConfigFile(Constants.CONFIG_DATA_FILENAME);
			ConfigData = MyGame.Manager.FileManager.LoadXml<ConfigData>(file);
		}
		public void LoadSnakeData()
		{
			String file = GetGlobalConfigFile(Constants.SNAKE_DATA_FILENAME);
			SnakeData = MyGame.Manager.FileManager.LoadXml<SnakeData[]>(file);
		}

		public BoardType BoardType { get; private set; }
		public ConfigData ConfigData { get; private set; }
		public SnakeData[] SnakeData { get; private set; }

		private static String GetGlobalConfigFile(String configFile)
		{
			return GetConfigFile(GetGlobalBaseContentRoot(), configFile);
		}
		private static String GetConfigFile(String root, String file)
		{
			return String.Format("{0}/{1}/{2}", root, Constants.DATA_DIRECTORY, file);
		}
	}
}